package com.example.whever.lacation;

import androidx.annotation.NonNull;

import com.example.whever.AppConfig;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.Objects;


import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class LocationManager {
    private static final OkHttpClient OK_HTTP_CLIENT = new OkHttpClient();
    public static final Gson GSON = new Gson();

    public static void queryLocation(String keywords, LocationApiCallback callback) {
        HttpUrl httpUrl = new HttpUrl.Builder()
                .scheme("https")
                .host("restapi.amap.com")
                .addEncodedPathSegments("v3/config/district")
                .addEncodedQueryParameter("key", AppConfig.A_MAP_API_KEY)
                .addEncodedQueryParameter("keywords", keywords)
                .build();
        Request request = new Request.Builder()
                .url(httpUrl)
                .build();
        OK_HTTP_CLIENT.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {

            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                String json = Objects.requireNonNull(response.body()).string();
                callback.onSuccess(GSON.fromJson(json, LocationApiResponse.class));
            }
        });
    }
}
